package org.cargloss.cp.controller.modul.user;

import java.sql.Timestamp;

import org.cargloss.cp.model.User;


import util.hibernate.DAO;
import util.secure.Enkripsi;

import com.opensymphony.xwork2.ActionSupport;

public class ActionJQGridCrud extends ActionSupport {
	private static final long serialVersionUID = -5933849517817526086L;
	private String id;

	private String password;
	private String userName;

	private String fullName;
	private String email;
	private Timestamp lastLogin;
	private String defaultPassword;
	private String oper;

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String execute() {
		if (getOper().equals("edit")) {
			return update();
		} else if (getOper().equals("add")) {
			return insert();
		} else {
			return delete();
		}
	}

	public String insert() {
		DAO<Integer> dao = new DAO<Integer>();

		User insert = new User();
		insert.setUserName(getUserName());
		Enkripsi enkripsi = new Enkripsi();
		insert.setPassword(enkripsi.md5(getPassword()));
		dao.insert(insert);
		dao.close();
		return SUCCESS;
	}

	public String update() {
		DAO<Integer> dao = new DAO<Integer>();
		User update = (User) dao.find(User.class, Integer.valueOf(getId()));
		update.setUserName(getUserName());
		Enkripsi enkripsi = new Enkripsi();
		update.setPassword(enkripsi.md5(getPassword()));

		dao.update(update);
		dao.close();
		return SUCCESS;
	}

	public String delete() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.delete(User.class, Integer.valueOf(getId()));
		dao.close();
		return SUCCESS;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Timestamp getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Timestamp lastLogin) {
		this.lastLogin = lastLogin;
	}

	public String getDefaultPassword() {
		return defaultPassword;
	}

	public void setDefaultPassword(String defaultPassword) {
		this.defaultPassword = defaultPassword;
	}
	
	
}
